<?php

namespace app\admin\model\building;

use app\admin\model\Building;
use app\admin\model\BuildingRoomBoardModel;
use app\admin\model\BuildingRoomFixedModel;
use app\admin\model\BuildingRoomPtModel;
use app\admin\model\BuildingZoneModel;
use app\admin\model\House;
use think\Model;


class Room extends Model
{

    // 表名
    protected $name = 'building_room';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    // protected $createTime = false;
    // protected $updateTime = false;
    // protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'create_time_text',
        'update_time_text',
        'delete_time_text'
    ];

    public function getQuery($scope = [])
    {
        $query = parent::getQuery($scope);
        $user_id = session('admin.id');
        if ($user_id) {
            $query->where('user_id', $user_id);
        }
        return $query;
    }

    public function getStatusList()
    {
        return [0 => '空置', 1 => '招租中', 2 => '待签约', 3 => '已签约'];
    }

    public function getTypeList()
    {
        return [
            1 => '居住',
            2 => '商铺',
            3 => '办公',
            4 => '厂房',
            5 => '仓库',
            6 => '建材',
            7 => '物业',
            8 => '车位',
        ];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getDeleteTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['delete_time']) ? $data['delete_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDeleteTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function buildingName()
    {
        return $this->hasOne(Building::class, 'id', 'build_id')->field('id, name as build_name')->bind('build_name');
    }

    public function zone()
    {
        return $this->hasOne(BuildingZoneModel::class, 'id', 'zone_id')->field('id, name as zone_name')->bind('zone_name');
    }

    public function house()
    {
        return $this->hasOne(House::class, 'room_id', 'id');
    }

    public function building()
    {
        return $this->belongsTo(Building::class, 'build_id', 'id');
    }

    public function board()
    {
        return $this->hasMany(BuildingRoomBoardModel::class, 'room_id', 'id');
    }

    public function fixed()
    {
        return $this->hasMany(BuildingRoomFixedModel::class, 'room_id', 'id');
    }

    public function pt()
    {
        return $this->hasMany(BuildingRoomPtModel::class, 'room_id', 'id');
    }

}
